#define __ctrl(vctrl) _ctrl = _XD_display displayCtrl vctrl
private ["_ctrl","_ok","_units","_index","_XD_display","_children","_nb","_entry_side","_side_name","_side_displayName","_side","_entry_faction","_faction_name","_faction_displayName","_nb_groups","_entry_group"
		,"_group_name","_group_displayName","_arr","_nb_child","_entry_u_group","_u_group_name","_u_group_displayName","_arr2","_i"];
if (!X_Client) exitWith {};

if (isnil "AI_GROUP_INSTANCE") then {
	AI_GROUP_INSTANCE = grpNull;
};

if (isnil "SIDE_AI_GROUP_ARRAY") then {
	_unit_class_indexer = [];
	_unit_class_array = [];
	
	_nb = count(configFile >> "CfgVehicles");
	for [{ _i = 0}, { _i < _nb}, { _i = _i + 1}] do {
		if ( isClass( (configFile >> "CfgVehicles") select _i) ) then{
			_entry = (configFile >> "CfgVehicles") select _i;
			if ( isNumber( _entry >> "scope" )) then {
				_scope = getNumber( _entry >> "scope");
				if (_scope == 2) then{
					_name = configName _entry;
					_side = getNumber( _entry >> "side");
					_faction = getText( _entry >> "faction");
					if (_faction == "BIS_BAF") then {
						_faction = "BIS_BAF_MTP";
					};
					_kindof = getText( _entry >> "vehicleclass");
					_displayName = getText( _entry >> "displayName");
					if( _name isKindOf "CAManBase") then {
						_kindof = "MAN";
						_arr = [];
						_arr set [0, _side];
						_arr set [1, _faction];
						_arr set [2, _kindof];
						_arr set [3, _name];
						_arr set [4, _displayName];
						_unit_class_indexer set [count _unit_class_indexer,_name];
						_unit_class_array set [count _unit_class_array, _arr];
					};
				};
			};
		};
	};
	
	SIDE_AI_GROUP_ARRAY = [];
	_nb = count (configFile >> "CfgGroups");
	for [{_i = 0}, {_i < _nb}, {_i = _i + 1}] do {
		if (isClass ((configFile >> "CfgGroups") select _i)) then {
			_entry_side = ((configFile >> "CfgGroups") select _i);
			_side_name = configName _entry_side;
			_side_displayName = getText(_entry_side >> "name");
			_side = switch (_side_name) do {
				case "East" : {"EAST"};
				case "West" : {"WEST"};
				case "Guerrila" : {"GUER"};
				case "Civilian" : {"CIV"};
				default  {"UNKNOWN"};
			};
			_side_id = switch (_side_name) do {
				case "East" : {0};
				case "West" : {1};
				case "Guerrila" : {2};
				case "Civilian" : {3};
				default  {-1};
			};
			if (_side == d_own_side) then {
				_nb_factions = count _entry_side;
				for [{_z = 0}, {_z < _nb_factions}, {_z = _z + 1}] do {
					if (isClass (_entry_side select _z)) then {
						_entry_faction = (_entry_side select _z);
						_faction_name = configName _entry_faction;
						_faction_displayName = getText(_entry_faction >> "name");
						_arr = [];
						_arr set [count _arr, _side];
						_arr set [count _arr, _side_id];
						_arr set [count _arr, _side_name];
						_arr set [count _arr, _faction_name];
						_arr set [count _arr, _faction_displayName];
						diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"grouprequest.sqf","Build Faction",_arr];
						_nb_groups = count _entry_faction;
						_child_group = [];
						for [{_x = 0}, {_x < _nb_groups}, {_x = _x + 1}] do {
							if (isClass (_entry_faction select _x)) then {
								_entry_group = (_entry_faction select _x);
								if (isClass (_entry_group)) then {
									_group_name = configName _entry_group;
									_group_displayName = getText(_entry_group >> "name");
									_arr2 = [];
									_arr2 set [count _arr2, _side];
									_arr2 set [count _arr2, _faction_name];
									_arr2 set [count _arr2, _faction_displayName];
									_arr2 set [count _arr2, _group_name];
									_arr2 set [count _arr2, _group_displayName];
									if (toupper(_group_name) == "INFANTRY") then { 
										diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"grouprequest.sqf","Build Group",_arr2];
										_nb_child = count _entry_group;
										_child_unit_group = [];
										for [{_y = 0}, {_y < _nb_child}, {_y = _y + 1}] do {
											if (isClass (_entry_group select _y)) then {
												_entry_u_group = (_entry_group select _y);
												_u_group_name = configName _entry_u_group;
												_u_group_displayName = getText(_entry_u_group >> "name");
												_arr3 = [];
												_arr3 set [count _arr3, _side];
												_arr3 set [count _arr3, _faction_name];
												_arr3 set [count _arr3, _group_name];
												_arr3 set [count _arr3, _u_group_name];
												_arr3 set [count _arr3, _u_group_displayName];
												diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"grouprequest.sqf","Build Units",_arr3];

												_nb_units = count _entry_u_group;
												_child_unit = [];
												for [{_z = 0}, {_z < _nb_units}, {_z = _z + 1}] do {
													if (isClass (_entry_u_group select _z)) then {
														_entry_unit = (_entry_u_group select _z);
														_unit_name = configName _entry_unit;
														_unit_type = getText(_entry_unit >> "vehicle");
														_arr4 = [];
														_arr4 set [count _arr4, _side];
														_arr4 set [count _arr4, _faction_name];
														_arr4 set [count _arr4, _group_name];
														_arr4 set [count _arr4, _unit_name];
														_arr4 set [count _arr4, _unit_type];
														
														_idx = _unit_class_indexer find _unit_type;
														if (_idx >= 0) then {
															_arr4 set [count _arr4, (_unit_class_array select _idx)];
														};
														
														_child_unit set [count _child_unit,_arr4];
														diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"grouprequest.sqf","Build Unit",_arr4];
													};
												};
												_arr3 set [count _arr3 ,_child_unit];
												_child_unit_group set [count _child_unit_group,_arr3];
											};
										};
										_arr set [count _arr, _child_unit_group];
									};
								};
							};
							_child_group set [count _child_group, _arr2];
						};
						SIDE_AI_GROUP_ARRAY set [count SIDE_AI_GROUP_ARRAY,_arr];
					};
				};		
			};
		};
	};
};

if (isnil "XfGroupDialogAddUnit") then {
	XfGroupDialogAddUnit = {
		private ["_XD_display","_ctrl_faction","_ctrl_group","_ctrl_unit","_faction_index","_group_index","_unit_index","_group_list","_unit_list"
				 ,"_unit_array","_lst_unit","_unit_class","_unit_displayName"];
				 
		if ((count D_AI_GROUP_UNIT_LIST) < d_maximum_units_in_ai_group) then {
			_XD_display = uiNameSpace getVariable "X_AI_GROUP_DIALOG";
			_ctrl_faction = _XD_display displayCtrl 23003; // Faction Combo
			_ctrl_group = _XD_display displayCtrl 23006; // Group Combo
			_ctrl_unit = _XD_display displayCtrl 23009; // Unit Combo

			_lst_unit = _XD_display displayCtrl 23010; // Unit list

			_faction_index = lbCurSel _ctrl_faction;
			_group_index = lbCurSel _ctrl_group;
			_unit_index = lbCurSel _ctrl_unit;
			
			_group_list = (SIDE_AI_GROUP_ARRAY select _faction_index) select 5;
			_unit_list = (_group_list select _group_index) select 5;
			_unit_array = _unit_list select _unit_index;
			
			D_AI_GROUP_UNIT_LIST set [ count D_AI_GROUP_UNIT_LIST, _unit_array];
			_unit_class = _unit_array select 5;
			_unit_displayName = _unit_class select 4;
			_lst_unit lbAdd _unit_displayName;
		} else {
			player sideChat format[localize "MSG_DOM_MAXGROUPUNIT", d_maximum_units_in_ai_group];
		};
	};
};

if (isnil "XfGroupDialogCreateGroup") then {
	XfGroupDialogCreateGroup = {
		private ["_unit_array","_unit_type"];
		if (isnull AI_GROUP_INSTANCE) then {
			AI_GROUP_INSTANCE = createGroup d_side_player;
			[player] joinSilent AI_GROUP_INSTANCE;
		};
		// player sideChat format["Group Created: %1", AI_GROUP_INSTANCE];
		{
			_unit_array = _x;
			_unit_type = _unit_array select 4;
			_unit_displayName = (_unit_array select 5) select 4;
			player sideChat format[localize "MSG_DOM_ADDEDGROUPUNIT", _unit_displayName];
			[_unit_type, AI_GROUP_INSTANCE, (getPos player), 10, "", "PRIVATE", 0.7] call XfCreateUnit;
		} foreach D_AI_GROUP_UNIT_LIST;
		D_AI_GROUP_UNIT_LIST = [];
	};
};

disableSerialization;

_ok = createDialog "XD_AIGroupDialog";

_XD_display = uiNameSpace getVariable "X_AI_GROUP_DIALOG";

diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"grouprequest.sqf","Start",[]];
_ctrl_faction = _XD_display displayCtrl 23003; // Faction Combo
lbClear 23003;
_ctrl_group = _XD_display displayCtrl 23006; // Group Combo
lbClear 23006;
_ctrl_unit = _XD_display displayCtrl 23009; // Unit Combo
lbClear 23009;
{
	_faction_displayName = _x select 3;
	_ctrl_faction lbAdd _faction_displayName;
} forEach SIDE_AI_GROUP_ARRAY;
_ctrl_faction lbSetCurSel 0;

D_AI_GROUP_UNIT_LIST = [];
waitUntil { !dialog || !alive player};
if (alive player && count D_AI_GROUP_UNIT_LIST > 0) then {
	
};
if (!alive player) then {closeDialog 0};